package com.fc.example;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.anythink.core.api.ATAdInfo;
import com.anythink.core.api.AdError;
import com.anythink.splashad.api.ATSplashAd;
import com.anythink.splashad.api.ATSplashAdExtraInfo;
import com.anythink.splashad.api.ATSplashAdListener;

import java.util.HashMap;
import java.util.Map;

public class SplashActivity extends AppCompatActivity {

    public String mMediaId; // 融合广告位

    private FrameLayout mSplashContainer;

    ATSplashAd splashAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View decoreView = getWindow().getDecorView();
        decoreView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

        setContentView(R.layout.activity_splash);
        this.mMediaId = "b65a7a929ea9df";
        mSplashContainer = findViewById(R.id.fl_content);
        // 加载并展示广告
        loadAndShowSplashAd();
    }

    private void loadAndShowSplashAd() {
        Toast.makeText(SplashActivity.this,"开始加载",Toast.LENGTH_SHORT).show();
        splashAd = new ATSplashAd(this, mMediaId, new ATSplashAdListener() {
            @Override
            public void onAdLoaded(boolean b) {
                Log.d("test", "onAdLoaded:");
                Toast.makeText(SplashActivity.this,"加载成功",Toast.LENGTH_SHORT).show();
                showAds();
            }

            @Override
            public void onAdLoadTimeout() {
                Log.d("test", "onAdLoadTimeout:");
            }

            @Override
            public void onNoAdError(AdError adError) {
                Log.d("test", "onNoAdError:");
                Toast.makeText(SplashActivity.this,"加载失败:" + adError.toString(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onAdShow(ATAdInfo atAdInfo) {
                Log.d("test", "onAdShow:");
            }

            @Override
            public void onAdClick(ATAdInfo atAdInfo) {
                Log.d("test", "atAdInfo:");
            }

            @Override
            public void onAdDismiss(ATAdInfo atAdInfo, ATSplashAdExtraInfo atSplashAdExtraInfo) {
                Log.d("test", "onAdDismiss:");
                SplashActivity.this.finish();
            }
        });
        Map<String, Object> localMap = new HashMap<>();
//        localMap.put(ATAdConst.KEY.AD_WIDTH, ScreenUtil.getScreenWidth(this)); // 单位px
//        localMap.put(ATAdConst.KEY.AD_HEIGHT, ScreenUtil.getScreenHeight(this)); // 单位px

        splashAd.setLocalExtra(localMap);
        splashAd.loadAd();
    }

    private void showAds() {
        // 广告加载监听器
        if (splashAd.isAdReady()) {
            //container大小至少占屏幕75%
            splashAd.show(this, mSplashContainer);
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        /** 6、在onDestroy中销毁广告 */

    }
}